package codeTop;

public class q498 {
    public int[] findDiagonalOrder(int[][] mat) {
        int num = 0;
        int m = mat.length;
        int n = mat[0].length;
        int i = 0,j = 0;
        boolean flag = true;
        int[] res = new int[m*n];
        while (num < n*m) {
            res[num] = mat[i][j];
            if (flag) {
                if (i - 1 >= 0 && j + 1 < n) {
                    i--;
                    j++;
                }else {
                    if (j + 1 == n) {
                        i++;
                    }else {
                        j++;
                    }
                    flag = false;
                }
            }else {
                if (i + 1 < m && j - 1 >= 0) {
                    i++;
                    j--;
                }else {
                    if (i + 1 == m) {
                        j++;
                    }else {
                        i++;
                    }
                    flag = true;
                }
            }
            num++;
        }
        return res;
    }
}
